package com.manage.modules.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.manage.modules.utils.MathUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.manage.modules.bo.TBaseDataRecordAddBo;
import com.manage.modules.bo.TBaseDataRecordQueryBo;
import com.manage.modules.bo.TBaseDataRecordEditBo;
import com.manage.modules.domain.TBaseDataRecord;
import com.manage.modules.mapper.TBaseDataRecordMapper;
import com.manage.modules.vo.TBaseDataRecordVo;
import com.manage.modules.service.ITBaseDataRecordService;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 数据记录Service业务层处理
 *
 * @author wx
 * @date 2021-09-14
 */
@Service
public class TBaseDataRecordServiceImpl extends ServiceImpl<TBaseDataRecordMapper, TBaseDataRecord> implements ITBaseDataRecordService {

    @Override
    public TBaseDataRecordVo queryById(Long dataId) {
        TBaseDataRecord db = this.baseMapper.selectById(dataId);
        return BeanUtil.toBean(db, TBaseDataRecordVo.class);
    }

    @Override
    public List<TBaseDataRecordVo> queryList(TBaseDataRecordQueryBo bo) {
        LambdaQueryWrapper<TBaseDataRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getDataType()), TBaseDataRecord::getDataType, bo.getDataType());
        lqw.eq(bo.getShouldReportStationNum() != null, TBaseDataRecord::getShouldReportStationNum, bo.getShouldReportStationNum());
        lqw.eq(bo.getRealReportStationNum() != null, TBaseDataRecord::getRealReportStationNum, bo.getRealReportStationNum());
        lqw.eq(bo.getMissReportStationNum() != null, TBaseDataRecord::getMissReportStationNum, bo.getMissReportStationNum());
        lqw.eq(bo.getOverTimeStationNum() != null, TBaseDataRecord::getOverTimeStationNum, bo.getOverTimeStationNum());
        lqw.eq(StrUtil.isNotBlank(bo.getRealReportStationIdCList()), TBaseDataRecord::getRealReportStationIdCList, bo.getRealReportStationIdCList());
        lqw.eq(StrUtil.isNotBlank(bo.getMissReportStationIdCList()), TBaseDataRecord::getMissReportStationIdCList, bo.getMissReportStationIdCList());
        lqw.eq(StrUtil.isNotBlank(bo.getOverTimeStationIdCList()), TBaseDataRecord::getOverTimeStationIdCList, bo.getOverTimeStationIdCList());
        lqw.eq(StrUtil.isNotBlank(bo.getTimes()), TBaseDataRecord::getTimes, bo.getTimes());
        lqw.eq(bo.getRequestNum() != null, TBaseDataRecord::getRequestNum, bo.getRequestNum());
        lqw.orderByDesc(true, TBaseDataRecord::getDataId);
        List<TBaseDataRecordVo> recordVoList = entity2Vo(this.list(lqw));
        // 计算比率
        for (TBaseDataRecordVo recordVo : recordVoList) {
            recordVo.setInTimeRate(MathUtils.except(recordVo.getInTimeStationNum(), recordVo.getShouldReportStationNum(), 2));
            recordVo.setRealRate(MathUtils.except(recordVo.getRealReportStationNum(), recordVo.getShouldReportStationNum(), 2));
            recordVo.setMissRate(MathUtils.except(recordVo.getMissReportStationNum(), recordVo.getShouldReportStationNum(), 2));
            recordVo.setOverTimeRate(MathUtils.except(recordVo.getOverTimeStationNum(), recordVo.getShouldReportStationNum(), 2));
        }
        return recordVoList;
    }

    /**
     * 实体类转化成视图对象
     *
     * @param collection 实体类集合
     * @return
     */
    private List<TBaseDataRecordVo> entity2Vo(Collection<TBaseDataRecord> collection) {
        List<TBaseDataRecordVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, TBaseDataRecordVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<TBaseDataRecord> page = (Page<TBaseDataRecord>) collection;
            Page<TBaseDataRecordVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page, pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    @Override
    public Boolean insertByAddBo(TBaseDataRecordAddBo bo) {
        TBaseDataRecord add = BeanUtil.toBean(bo, TBaseDataRecord.class);
        validEntityBeforeSave(add);
        return this.save(add);
    }

    @Override
    public Boolean updateByEditBo(TBaseDataRecordEditBo bo) {
        TBaseDataRecord update = BeanUtil.toBean(bo, TBaseDataRecord.class);
        validEntityBeforeSave(update);
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(TBaseDataRecord entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return this.removeByIds(ids);
    }
}
